import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    //全局存储数据的地方
    isLogin: localStorage.getItem("loginKey") ? true : false,
    userinfo: localStorage.getItem("loginKey"),
  },
  getters: {},
  mutations: {
    //专门用来同步更改 state 区域的地方
    LOGIN(state, payload) {
      state.isLogin = true;
      state.userinfo = payload.data;  
      localStorage.setItem("loginKey", payload.data);
    },
    LOGOUT(state) {
      state.isLogin = false;
      state.userinfo = {};
      localStorage.removeItem("loginKey");
    },
  },
  actions: {
    //也是用来异步更改state的地方
    login(context, payload) {
      context.commit("LOGIN", payload);
    },
    logout(context) {
      setTimeout(() => {
        context.commit("LOGOUT"); //三秒后注销
      }, 3000);
    },
  },
  modules: {},
});
